<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Cambio_clave extends MN_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->helper('url');
        
    }

    public function edit_user() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();

        $this->data['title'] = "Editar Usuario";

        //if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) {
        //    redirect('auth', 'refresh');
        //}
        //$this->data['user'] = $this->ion_auth->get_user_username();
        $user_id=$this->ion_auth->get_user_id();
        //print_r($user_id);

        $user = $this->ion_auth->user($user_id)->row();



        //set the flash data error message if there is one
        //$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

        //pass the user to the view
        
        
        $this->data['users'] = $user;
        $this->data['first_name'] = $user->first_name;
        $this->data['last_name'] = $user->last_name;
        //print_r($user);
         $this->_render('cambio_clave/new');


        //$this->_render_page('auth/edit_user', $this->data);
    }
    public function save_user() {
        if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        //$this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->data['user'] = $this->ion_auth->get_user_username();
        $user_id=$this->ion_auth->get_user_id();
         $user = $this->ion_auth->user($user_id)->row();


        //process the phone number

        //validate form input
        if (isset($_POST) && !empty($_POST)) {
            $data = array(
                'first_name' =>$user->first_name,
                'last_name' =>$user->last_name,
                'company' =>$user->company,
                'phone' => $user->phone,
            );

            //Update the groups user belongs to

            //update the password if it was posted
            if ($this->input->post('password')) {
                $this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
                $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');

                $data['password'] = $this->input->post('password');
               
            }

            if ($this->form_validation->run() === TRUE) {
                $this->ion_auth->update($user->id, $data);

                //check to see if we are creating the user
                //redirect them back to the admin page
                $this->session->set_flashdata('message', "User Saved");
                //print
                //redirect("auth", 'refresh');
                echo "clave se ha modificado";
                $this->data['msj_del']="SE ACTUALIZO SU CLAVE DE ACCESO";
                $this->data['tit_del']="ACTUALIZAR CLAVE";
                $this->data['volver']="docente/inicio";

                $this->_render('template/msj_error');
                
            }else{
                $this->data['users'] = $user;
                $this->data['first_name'] = $user->first_name;
                $this->data['last_name'] = $user->last_name;
                $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
                
                 $this->_render('cambio_clave/new');
            }
        }

    }    
}
